package saeildamil.ch18.dbconn;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import javax.swing.JOptionPane;

public class JdbcEX01 {
	public static void main(String[] args) {
		Connection con = null;
			con = dbCon();
			JOptionPane.showMessageDialog(null, "데이터베이스 연결에 성공했습니다.");
			try {con.close();} catch (SQLException e) {}
	}

	private static Connection dbCon() {
		Connection conn = null;
		BufferedReader bis = null;
		File file = new File("dbinfo.con");
		try {
			bis = new BufferedReader(new FileReader(file));
			String className = bis.readLine();
			String url = bis.readLine();
			String user = bis.readLine();
			String password = bis.readLine();
			Class.forName(className);
			conn = DriverManager.getConnection(url, user, password);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} finally {
			try {
				bis.close();
			} catch (IOException e) {}
		}
		return conn;
	}
}
